package edu.cuit.avatar.callback;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/11 8:41
 */
public class LocalInnerClassTalking {
    private int interval = 1000;
    private boolean beep = false;

    public LocalInnerClassTalking() {
    }

    public LocalInnerClassTalking(int interval, boolean beep) {
        this.interval = interval;
        this.beep = beep;
    }

    public void start(){
        class BeepActionListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                //该事件发生时发出一个通告
                System.out.println("At the tone，the time is "
                        + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
                //内部类可以直接访问外围类的成员
                if(beep)
                    //伴随响铃
                    Toolkit.getDefaultToolkit().beep();
            }
        }

        Timer timer = new Timer(1000, new BeepActionListener());
        timer.start();
    }
}
